package com.shunwang.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.shunwang.vo.FileOptResult;
import com.shunwang.vo.Item;
import com.shunwang.vo.OptResult;
import com.shunwang.vo.Page;
import com.shunwang.webtools.spring.velocity.controller.InterceptorMultiActionController;

@Controller
public class TestController extends InterceptorMultiActionController {

	private String basePath;
	
	private String webPath;
	
	private ModelAndView view = new ModelAndView("json");
	
	private List<Item> row1 = new ArrayList<Item>(){{
		this.add(new Item("01","新闻"));this.add(new Item("02","专题"));this.add(new Item("03","攻略"));this.add(new Item("04","评测"));
		this.add(new Item("0501","玩家扮演"));this.add(new Item("0502","壁纸原画"));this.add(new Item("0503","游戏截图"));
		this.add(new Item("0505","网友恶搞"));this.add(new Item("0601","电竞赛事"));this.add(new Item("0504","游戏周边"));
		
	}};
	private List<Item> row2 = new ArrayList<Item>(){{
		this.add(new Item("09","未知栏目"));this.add(new Item("10","全站头条"));this.add(new Item("11","首页推荐"));this.add(new Item("12","图片新闻"));
		this.add(new Item("13","推荐活动"));this.add(new Item("0602","预告宣传"));this.add(new Item("0603","攻略教学"));
		this.add(new Item("0604","趣味原创"));this.add(new Item("07","活动"));this.add(new Item("08","资料"));
		
	}};
	private List<Item> row3 = new ArrayList<Item>(){{
		this.add(new Item("14","首页轮播"));this.add(new Item("15","xxx题"));
	}};
	
	private final Map<Integer,List<Item>> map = new HashMap<Integer,List<Item>>(){{
		this.put(1, row1);
		this.put(2, row2);
		this.put(3, row3);
	}};
	
	@RequestMapping(value="upFile",method = RequestMethod.POST)
	public ModelAndView upFile(HttpServletRequest request,HttpServletResponse response,@RequestParam("up") MultipartFile file){
		File destFile = new File(basePath);
		if(!destFile.exists()){
			destFile.mkdirs();
		}
		
		try {
			file.transferTo(new File(destFile,file.getOriginalFilename()));
			request.setAttribute("json", new FileOptResult(true,webPath+file.getOriginalFilename()));
		} catch (IOException e) {
			request.setAttribute("json", new FileOptResult(false,webPath+file.getOriginalFilename()));
		}
		response.setContentType("text/html");
		return view;
	}
	
	@RequestMapping(value="paging")
	public ModelAndView paging(HttpServletRequest request,HttpServletResponse response,QueryItem item){
		Integer page = null;
		String id = null;
		String name = null;
		if(page == null){
			page = 1;
		}
		if(item != null){
			if(item.getId()!= null && item.getId().trim().length() > 0){
				id= item.getId().trim();
			}
			if(item.getName() != null && item.getName().trim().length() > 0){
				name = item.getName().trim();
			}
			if(item.getPage() != null){
				page = item.getPage();
			}
		}
		List<Item> content = null;
		if(name == null && id == null){
			content = map.get(page);
		}else{
			content = new ArrayList<Item>();
			List<Item> list = getItems(id,name);
			if(list != null && list.size() > 0){
				content.addAll(list);
			}
		}
		request.setAttribute("json", new Page(content));
		return view;
	}
	@RequestMapping(value="addOrUpd")
	public ModelAndView addOrUpd(HttpServletRequest request,HttpServletResponse response,Item item){
		if(item.getId() != null && item.getId().trim().length() > 1){
			List<Item> items = getItems(item.getId(),null);
			if(items.size() > 0){
				Item tmp = items.get(0);
				tmp.setName(item.getName());
			}
		}else{
			row3.add(item);
		}
		request.setAttribute("json", new OptResult(true));
		return view;
	}
	
//	public String doProcess(){
//		getRequest().setAttribute("json", "{success:true,msg:'ok'}");
//		return SUCCESS;
//	}
	
	
	
	
	private List<Item> getItems(String id,String name) {
		List<Item> result = new ArrayList();
		getRow(id,name,row1,result);
		getRow(id,name,row2,result);
		getRow(id,name,row3,result);
		return result;
	}

	private void getRow(String id, String name, List<Item> row12,
			List<Item> result) {
		for(Item item : row12){
			if(id != null && name != null){
				if(item.getId().equals(id) && item.getName().equals(name)){
					result.add(item);
				}
			}else if(id != null){
				if(item.getId().equals(id)){
					result.add(item);
				}
			}else if(name != null){
				if(item.getName().equals(name)){
					result.add(item);
				}
			}
		}
	}


	public String getBasePath() {
		return basePath;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public String getWebPath() {
		return webPath;
	}

	public void setWebPath(String webPath) {
		this.webPath = webPath;
	}

}
